package com.breeze.blockchain.bc.controller;

import com.alibaba.fastjson2.JSON;
import com.breeze.blockchain.bc.service.BlockService;
import com.breeze.blockchain.bc.service.PowService;
import com.breeze.blockchain.bc.util.BlockCache;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author breeze
 */
@RestController
@RequestMapping("bc")
public class BlockController {

  @Resource BlockService blockService;

  @Resource PowService powService;

  @Resource BlockCache blockCache;

  /** 查看当前节点区块链数据 */
  @GetMapping("/scan")
  public String scanBlock() {
    return JSON.toJSONString(blockCache.getBlockChain());
  }

  /** 查看当前节点区块链数据 */
  @GetMapping("/data")
  public String scanData() {
    return JSON.toJSONString(blockCache.getPackedTransactions());
  }

  /** 创建创世区块 */
  @GetMapping("/create")
  public String createFirstBlock() {
    blockService.createGenesisBlock();
    return JSON.toJSONString(blockCache.getBlockChain());
  }

  /** 工作量证明PoW 挖矿生成新的区块 */
  @GetMapping("/mine")
  public String createNewBlock() {
    powService.mine();
    return JSON.toJSONString(blockCache.getBlockChain());
  }
}
